home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / unix / getcwd.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  2KB  |  88 lines

  1.  
  2. /*
  3.  *  GETCWD.C
  4.  *
  5.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  6.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  7.  *    DICE-LICENSE.TXT.
  8.  */
  9.  
  10. #include <exec/types.h>
  11. #include <libraries/dos.h>
  12. #include <libraries/dosextens.h>
  13. #include <clib/exec_protos.h>
  14. #include <clib/dos_protos.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18.  
  19. typedef struct FileInfoBlock FIB;
  20. typedef struct Process Process;
  21.  
  22. char *
  23. getcwd(path, max)
  24. char *path;
  25. int max;
  26. {
  27.     BPTR lock;
  28.     FIB *fib;
  29.     int len = 0;    /*    path length    */
  30.     int elen;        /*    element length    */
  31.     short alloced = 0;
  32.  
  33.     {
  34.     Process *proc = (Process *)FindTask(NULL);
  35.     if (proc->pr_Task.tc_Node.ln_Type == NT_TASK)
  36.         return(NULL);
  37.     lock = DupLock(proc->pr_CurrentDir);
  38.     if (lock == NULL)
  39.         return(NULL);
  40.     }
  41.     if (path == NULL) {
  42.     path = malloc(max);
  43.     if (path == NULL)
  44.        max = 0;    /* Don't let the overwrite non-existent memory */
  45.     alloced = 1;
  46.     }
  47.  
  48.     if (fib = malloc(sizeof(FIB))) {
  49.     while (lock && Examine(lock, fib)) {
  50.         BPTR newlock;
  51.  
  52.         elen = strlen(fib->fib_FileName);
  53.         if (len + elen + 2 > max)
  54.         break;
  55.  
  56.         newlock = ParentDir(lock);
  57.  
  58.         if (len) {
  59.         if (newlock)
  60.             strins(path, "/");
  61.         else
  62.             strins(path, ":");
  63.         strins(path, fib->fib_FileName);
  64.         ++len;
  65.         } else {
  66.         strcpy(path, fib->fib_FileName);
  67.         if (newlock == NULL)
  68.             strcat(path, ":");
  69.         }
  70.         len += elen;
  71.  
  72.         UnLock(lock);
  73.         lock = newlock;
  74.     }
  75.     free(fib);
  76.     }
  77.  
  78.     if (lock) {
  79.     UnLock(lock);
  80.     if (alloced)
  81.         free(path);
  82.     path = NULL;
  83.     }
  84.     return(path);
  85. }
  86.  
  87.  
  88.